#    LOL Man pages
#    Copyright (C) 2010  kishore@logonlinux.com
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
import collections
import cgi
import os
import datetime
import re
import urllib
import logging
import os

import markdown
import simplejson

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.api import users
from functools import wraps

md = markdown.Markdown() 

# BaserequestHandler
class BaseRequestHandler(webapp.RequestHandler):
    def generate(self, template_name, template_values={}):               
        values = {
            'request': self.request,
            'uri':self.request.url.replace(self.request.path+self.request.query_string,""),
            'user': users.get_current_user(),
            'isAdmin':users.is_current_user_admin(),
            'login_url': users.create_login_url(self.request.uri),
            'logout_url': users.create_logout_url(self.request.uri),
            'application_name': 'Easy page'
        }
        values.update(template_values)
        directory = os.path.dirname(__file__)
        path = os.path.join(directory, os.path.join('templates', template_name))
        self.response.out.write(template.render(path, values,debug=True))

def markDown(text):
    return md.convert(text)

def requiresAdmin(method):
	@wraps(method)
	def wrapper(self, *args, **kwargs):
		user = users.get_current_user()
		if not user:
			self.redirect(users.CreateLoginURL(self.request.uri))
			return
		elif users.is_current_user_admin():
			method(self, *args, **kwargs)
			return
		else:
			self.generate("error.html",{"error":"Permission denied"})
			return 
	return wrapper	
